<?php
        if (!current_user_can('manage_options'))
		{
			wp_die('You do not have sufficient permissions to access this page.');
		}
        $store_prefix = get_option('cpx_store_prefix');
        $aff_out_link_old = get_option('aff_out_link_prefix');
        $aff_out_link_prefix_cp = get_option('aff_out_link_prefix_cp');
        $test_in_localhost = get_option('test_in_localhost');
        $email_contact = get_option('email_contact');
        $email_new_coupon = get_option('email_new_coupon');
        $emto_subscriber = get_option('emto_subscriber');
        $store_slug = get_option('store_slug');
?>
        <div class="wrap">
        <?php screen_icon('themes'); ?> <h2>General</h2>
        <form method="POST" action="">
            <table class="form-table">
                <tr valign="top">
                    <th scope="row">
                        <label for="store_prefix">Default additional store name prefix</label>
                    </th>
                    <td>
                        <input value="<?php echo ($_POST['store_prefix']?$_POST['store_prefix']:$store_prefix); ?>" name="store_prefix" type="text" placeholder="ex: coupon, voucher, promotion code, etc. " style="width: 250px;"/>
                        ex: Udemy coupons
                    </td>
                </tr>
                <tr>
                    <th>
                        <label>Default prefix out affiliate link store</label>
                    </th>
                    <td>
                        <input value="<?php echo ($_POST['aff_out_link_prefix']?$_POST['aff_out_link_prefix']:$aff_out_link_old); ?>" name="aff_out_link_prefix" type="text" placeholder="ex: go, out, etc. " style="width: 250px;"/>
                        ex: http://domain.com/out/s0998
                    </td>
                </tr>
                <tr>
                    <th>
                        <label>Default prefix out affiliate link coupon</label>
                    </th>
                    <td>
                        <input value="<?php echo ($_POST['aff_out_link_prefix_cp']?$_POST['aff_out_link_prefix_cp']:$aff_out_link_prefix_cp); ?>" name="aff_out_link_prefix_cp" type="text" placeholder="ex: go, out, etc. " style="width: 250px;"/>
                        ex: http://domain.com/view-coupon/1234
                    </td>
                </tr>
                <tr>
                    <th>
                        <label>Run CouponX in localhost ?</label>
                    </th>
                    <td>
                        <input value="<?php echo ($_POST['test_in_localhost']?$_POST['test_in_localhost']:$test_in_localhost); ?>" name="test_in_localhost" type="text" placeholder="Enter number 1 to enable" style="width: 200px;"/>
                    </td>
                </tr>
                <tr>
                    <th>
                        <label>Email notification new contact to these addresses</label>
                    </th>
                    <td>
                        <input value="<?php echo ($_POST['email_contact']?$_POST['email_contact']:$email_contact); ?>" name="email_contact" type="text" placeholder="ex: mostcoupon@gmail.com,haiht369@gmail.com" style="width: 100%"/>
                    </td>
                </tr>
                <tr>
                    <th>
                        <label>Email notification new coupon submitted to these addresses</label>
                    </th>
                    <td>
                        <input value="<?php echo ($_POST['email_new_coupon']?$_POST['email_new_coupon']:$email_new_coupon); ?>" name="email_new_coupon" type="text" placeholder="ex: mostcoupon@gmail.com,haiht369@gmail.com" style="width: 100%"/>
                    </td>
                </tr>
                <tr>
                    <th>
                        <label>Enable notification new coupons to subscribers</label>
                    </th>
                    <td>
                        <input value="<?php echo ($_POST['emto_subscriber']?$_POST['emto_subscriber']:$emto_subscriber); ?>" name="emto_subscriber" type="text" placeholder="Enter number 1 to enable" style="width: 200px"/>
                    </td>
                </tr>
                <tr>
                    <th>
                        <label>Addition Store Slug</label>
                    </th>
                    <td>
                        <input value="<?php echo ($_POST['store_slug']?$_POST['store_slug']:$store_slug); ?>" name="store_slug" type="text" placeholder="ex: Default: coupons" style="width: 200px"/>
                        ex: http://domain.com/udemy-coupons
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="submit" value="Save settings" class="button-primary"/></td>
                </tr>
            </table>
        </form>
        </div>
<?php
        if(
            isset($_POST["store_prefix"])
            || isset($_POST['aff_out_link_prefix'])
            || isset($_POST['test_in_localhost'])
            || isset($_POST['aff_out_link_prefix_cp'])
            || isset($_POST['email_contact'])
            || isset($_POST['email_new_coupon'])
            || isset($_POST['emto_subscriber'])
            || isset($_POST['store_slug'])
        )
        {
            // update store prefix
            $new_prefix = esc_attr($_POST["store_prefix"]);
            update_option('cpx_store_prefix', $new_prefix);
            // update affiliate out link store
            $new_outlink_st = esc_attr($_POST["aff_out_link_prefix"]);
            update_option('aff_out_link_prefix', $new_outlink_st);
            // update aff_out_link_prefix_cp
            $new_outlink_cp = esc_attr($_POST["aff_out_link_prefix_cp"]);
            update_option('aff_out_link_prefix_cp', $new_outlink_cp);
            // update test in localhost or not
            $test_lc = esc_attr($_POST["test_in_localhost"]);
            update_option('test_in_localhost', $test_lc);
            // update emails receive new contact notification
            $em_contact = esc_attr($_POST["email_contact"]);
            update_option('email_contact', $em_contact);
            // update emails receive new coupon notification
            $em_new_cp = esc_attr($_POST["email_new_coupon"]);
            update_option('email_new_coupon', $em_new_cp);
            // enable send email to subscribers
            $em_to_subscribers = esc_attr($_POST["emto_subscriber"]);
            update_option('emto_subscriber', $em_to_subscribers);
            // Store Slug
            $addition_store_slug = esc_attr($_POST["store_slug"]);
            update_option('store_slug', $addition_store_slug);
            // print success message
            echo '<div id="message" class="updated">Settings saved</div>';
        }